import * as Api from "../../api/invoice";
import Notify from "@vant/weapp/notify/notify";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order_numbers: [],
    title_type: "company",
    total_amount: "",
    visableDialog: false,
    visibleTypeDialog: false,
    orderListLength: 0,
    type: [
      { id: "company", name: "企业" },
      { id: "person", name: "个人/非企业单位" },
    ],
    title: "",
    tax_no: "",
    remark: "",
    address: "",
    bank_account: "",
    bank_address: "",
    email: "",
    temp: {
      title_type: "company",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const _temp = wx.getStorageSync("_temp");

    if (options.total_amount) {
      this.setData({
        total_amount: options.total_amount,
        order_numbers: [options.order_numbers],
        orderListLength: 1,
      });
    } else {
      const orderList = wx.getStorageSync("checkedList");
      this.setData({
        order_numbers: orderList.map((i) => i.number),
        orderListLength: orderList.length,
        total_amount: parseFloat(
          orderList
            .reduce((sum, item) => sum + parseFloat(item.amount), 0)
            .toFixed(2)
        ),
      });
    }

    if (
      JSON.stringify(this.data.order_numbers) ===
      JSON.stringify(_temp.order_numbers)
    ) {
      this.setData({ ..._temp });
    }
  },
  // 自定义返回逻辑
  handleBackEvent() {
    this.onShowInvoiceOrders();
  },

  onShowTitleType() {
    this.setData({ visibleTypeDialog: true });
  },

  onChangeTitle(e) {
    this.setData({ title: e.detail.value });
  },
  onChangeTaxNo(e) {
    this.setData({ tax_no: e.detail.value });
  },
  onChangeRemark(e) {
    this.setData({ remark: e.detail.value });
  },
  onChangeAddress(e) {
    this.setData({ address: e.detail.value });
  },
  onChangeBankAccount(e) {
    this.setData({ bank_account: e.detail.value });
  },
  onChangeBankAddress(e) {
    this.setData({ bank_address: e.detail.value });
  },
  onChangeEmail(e) {
    this.setData({ email: e.detail.value });
  },
  onShowInvoiceOrders() {
    console.log("onShowInvoiceOrders");
    console.log("title", this.data.title);
    const {
      title,
      tax_no,
      email,
      remark,
      address,
      bank_account,
      bank_address,
      order_numbers,
    } = this.data;
    const _temp = {
      title,
      tax_no,
      email,
      remark,
      address,
      bank_account,
      bank_address,
      order_numbers,
    };
    wx.setStorageSync("_temp", _temp);
    wx.navigateBack({ delta: 1 });
  },

  /**
   * @description: 提交-唤起二次确认
   * @param {*} e
   * @return {*}
   */
  formSubmit(e) {
    console.log(e.detail.value);
    const { title, tax_no, email } = e.detail.value;
    if (!title) {
      Notify({
        message: "请输入发票抬头",
        top: 100,
      });
      return;
    }

    if (this.data.title_type === "company" && !tax_no) {
      Notify({
        message: "请输入税号",
        top: 100,
      });
      return;
    }

    if (!email) {
      Notify({
        message: "请输入邮箱",
        top: 100,
      });
      return;
    }
    this.setData({
      temp: { ...e.detail.value, title_type: this.data.title_type },
      visableDialog: true,
    });
  },
  onChangePicker(e) {
    this.setData({
      title_type: e.detail.value.id,
      visibleTypeDialog: false,
    });
  },
  onClose() {
    this.setData({ visableDialog: false, visibleTypeDialog: false });
  },
  submitBtn() {
    const { temp, order_numbers } = this.data;
    Api.submitInvoice({ order_numbers, invoice_content: temp }).then(() => {
      wx.removeStorageSync("checkedList");
      wx.removeStorageSync("_temp");
      wx.navigateTo({
        url: "/pages/invoiceSubmitSuccess/invoiceSubmitSuccess",
      });
    });
  },
});
